<?php

/**
 * konseling actions.
 *
 * @package    alazhar
 * @subpackage konseling
 * @author     Ardhy Widodo
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class konselingActions extends sfActions {

    public function __construct($context, $moduleName, $actionName) {
        parent::__construct($context, $moduleName, $actionName);
        $this->getUser()->setAttribute('subsystem', 'bk');
        $this->getUser()->setAttribute('module', 'konseling');
    }

    public function executeIndex(sfWebRequest $request) {
        $this->konselings = Doctrine_Core::getTable('Konseling')
                ->createQuery('a')
                ->execute();
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new KonselingForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new KonselingForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($konseling = Doctrine_Core::getTable('Konseling')->find(array($request->getParameter('id'))), sprintf('Object konseling does not exist (%s).', $request->getParameter('id')));
        $this->form = new KonselingForm($konseling);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($konseling = Doctrine_Core::getTable('Konseling')->find(array($request->getParameter('id'))), sprintf('Object konseling does not exist (%s).', $request->getParameter('id')));
        $this->form = new KonselingForm($konseling);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($konseling = Doctrine_Core::getTable('Konseling')->find(array($request->getParameter('id'))), sprintf('Object konseling does not exist (%s).', $request->getParameter('id')));
        $konseling->delete();

        $this->redirect('konseling/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $konseling = $form->save();

            $this->redirect('konseling/edit?id=' . $konseling->getId());
        }
    }

}
